<%@page import="powerpass.PowerPassSQL" %>
<%@page import="java.sql.*" %>
<div style="background-color: #000000;">
    <%

                System.out.println(request.getParameter("firstName"));
                System.out.println(request.getParameter("lastName"));
                System.out.println(request.getParameter("emailAddress"));
                System.out.println(request.getParameter("password"));
                System.out.println(request.getParameter("q1answer"));
                System.out.println(request.getParameter("q2answer"));

                //For some reason when I implemented this portion of the error checking before
                //the main phase, I kept getting a null pointer exception. Couldn't figure out why.
            /*
                boolean ownAnswer = false; //initializing

                if (request.getParameter("securityquestiondropdown2")==("5"))
                {
                if (request.getParameter("ownAnswer") != null)
                {
                if (!(request.getParameter("ownAnswer").trim().length()==0))
                {
                ownAnswer = true;
                } else {
                ownAnswer = false;
                }
                } else {
                ownAnswer = false;
                }
                } else {
                ownAnswer = true;
                }
                 */

                if (request.getParameter("firstName") != null
                        && request.getParameter("lastName") != null
                        && request.getParameter("emailAddress") != null
                        && request.getParameter("password") != null
                        && request.getParameter("q1answer") != null
                        && request.getParameter("q2answer") != null //&& ownAnswer
                        ) {

                    //This is strange. Tomcat refuses to acknowledge the isEmpty() method.
                    //I'm implementing a different strategy that should still work.
                    if (!(request.getParameter("firstName").trim().length() == 0)
                            && !(request.getParameter("lastName").trim().length() == 0)
                            && !(request.getParameter("emailAddress").trim().length() == 0)
                            && !(request.getParameter("password").trim().length() == 0)
                            && !(request.getParameter("q1answer").trim().length() == 0)
                            && !(request.getParameter("q2answer").trim().length() == 0)
                            && !(request.getParameter("securityquestiondropdown1").equals(request.getParameter("securityquestiondropdown2")))) {
                        String newSQL = "";
                        PowerPassSQL pp = new PowerPassSQL();

                        String sqlFirstName = (String) request.getParameter("firstName");
                        String sqlLastName = (String) request.getParameter("lastName");
                        String sqlEmailAddress = (String) request.getParameter("emailAddress");
                        String sqlPassword = (String) request.getParameter("password");
                        String sqlQ1Answer = (String) request.getParameter("q1answer");
                        String sqlQ2Answer = (String) request.getParameter("q2answer");
                        String sqlDropdown1 = (String) request.getParameter("securityquestiondropdown1");
                        String sqlDropdown2 = (String) request.getParameter("securityquestiondropdown2");
                        String sqlOwnAnswer = (String) request.getParameter("ownAnswer");

                        newSQL = "SELECT * FROM clients WHERE email_address ='" + sqlEmailAddress + "';";
                        ResultSet pash = pp.selectInfo(newSQL);
                        boolean isNewClient = pash.first();

                        if (request.getParameter("securityquestiondropdown2").equals("5")) {
                            newSQL = "INSERT INTO clients (firstname, lastname, password, email_address, q1id, q1answer, q2id, q2answer, histpasschangedate) "
                                    + "VALUES ('" + sqlFirstName + "', '" + sqlLastName + "', '" + sqlPassword + "', '" + sqlEmailAddress + "', '" + sqlDropdown1 + "', '" + sqlQ1Answer + "', '" + sqlDropdown2 + "', '" + sqlOwnAnswer + "', now());";
                        } else {
                            newSQL = "INSERT INTO clients (firstname, lastname, password, email_address, q1id, q1answer, q2id, q2answer, histpasschangedate) "
                                    + "VALUES ('" + sqlFirstName + "', '" + sqlLastName + "', '" + sqlPassword + "', '" + sqlEmailAddress + "', '" + sqlDropdown1 + "', '" + sqlQ1Answer + "', '" + sqlDropdown2 + "', '" + sqlQ2Answer + "', now());";
                        }

                        if (!(isNewClient)) {
                            if (!(request.getParameter("securityquestiondropdown2").equals("5") && (request.getParameter("ownAnswer").trim().length() == 0 || request.getParameter("ownAnswer") == null))) {
                                pp.insertInfo(newSQL);

                                out.write("Welcome " + sqlFirstName + " " + sqlLastName + "!");%><BR><%
                                                    out.write("Your Email Address: " + sqlEmailAddress);%><BR><%
                                                                                out.write("Your Password: " + sqlPassword);%><BR><%
                                                                                                        out.write("Please do not lose this information. Be sure to commit it to memory.");
                                                                                                        pp.Close();
                                                                                                        response.sendRedirect("login.jsp");
                                                }
                                            } else {
    %>This Client exists already.<BR><%
                                            }
                                        } else {
                                                String theMessage = "";
                                                pageContext.setAttribute("firstName", request.getAttribute("firstName"));
                                                pageContext.setAttribute("lastName", request.getAttribute("lastName"));
                                                pageContext.setAttribute("emailAddress", request.getAttribute("emailAddress"));

                                                if (request.getParameter("firstName").trim().length() == 0) {
                                                    theMessage += "Cannot leave First Name Blank<BR>";
                                                }
                                                if (request.getParameter("lastName").trim().length() == 0) {
                                                    theMessage += "Cannot leave Last Name blank<BR>";
                                                }
                                                if (request.getParameter("emailAddress").trim().length() == 0) {
                                                    theMessage += "Cannot leave Email Address blank<BR>";
                                                }
                                                if (request.getParameter("password").trim().length() == 0) {
                                                    theMessage += "Cannot leave Password blank<BR>";
                                                }
                                                if (request.getParameter("q1answer").trim().length() == 0) {
                                                    theMessage += "Cannot leave Security Question 1 blank<BR>";
                                                }
                                                if (request.getParameter("q2answer").trim().length() == 0) {
                                                    theMessage += "Cannot leave Security Question 2 blank<BR>";
                                                }
                                                if (request.getParameter("securityquestiondropdown2") == "5" && (request.getParameter("ownAnswer").trim().length() == 0 || request.getParameter("ownAnswer") == null)) {
                                                    theMessage += "Cannot leave Personal Answer blank if forming your own question<BR>";
                                                }
                                                if (request.getParameter("securityquestiondropdown1") == request.getParameter("securityquestiondropdown2")) {
                                                    theMessage += "You must pick two different security questions<BR>";
                                                }
                                                if (request.getParameter("confirmPassword") == request.getParameter("password")) {
                                                    theMessage += "The Password and Confirm password fields must match<BR>";
                                                }
                                                session.setAttribute("theMessage", theMessage);
                                                response.sendRedirect("login.jsp");
                                            }
                                        }
    %>
    <form id="myform" action="NewClient.jsp">
        <table
            <tr>
                <td >First Name:</td>
                <td ><input name="firstName" id="firstName" type="text" size="22"/></td>
            </tr>
            <tr>
                <td >Last Name:</td>
                <td ><input name="lastName" id="lastName" type="text" size="22"/></td>
            </tr>
            <tr>
                <td >Email Address:</td>
                <td ><input name="emailAddress" id="emailAddress" type="text" size="22"/></td>
            </tr>
            <tr>
                <td >Password:</td>
                <td ><input name="password" id ="passsword" type="text" size="22"/></td>
            </tr>
            <tr>
                <td >Confirm Password:</td>
                <td ><input name="confirmpassword" id ="confirmpassword" type="text" size="22" onblur="checkPasswordMatch()"/></td>
            </tr>
            <tr>
                <td><select name="securityquestiondropdown1">
                        <option value ="1">What is my favorite animal?</option>
                        <option value ="2">Where am I from?</option>
                        <option value ="3">What was my first pet's name?</option>
                        <option value ="4">What is your Mother's Maiden name?</option>
                    </select></td>
                <td><input name="q1answer" id ="q1answer" type="text" size="22"/></td>
            </tr>
            <tr>
                <td><select name="securityquestiondropdown2">
                        <option value ="1">What is my favorite animal?</option>
                        <option value ="2">Where am I from?</option>
                        <option value ="3">What was my first pet's name?</option>
                        <option value ="4">What is your Mother's Maiden name?</option>
                        <option value ="5">Make your own question!</option>
                    </select></td>
                <td><input name="q2answer" id ="q2answer" type="text" size="22"/></td>
            </tr>
            <tr>
                <td>If your own question, give answer:</td>
                <td><input name="ownAnswer" id="ownAnswer" type="text" size="22"/></td>
            </tr>
        </table>
        <table>
            <tr>
                <td><input name="reset" type="Reset" value="RESET"/></td>
                <td></td>
                <td><input name="submit" id="submit" type="Submit" value="SUBMIT"/></td>
                <td></td>
                <td><input name="submit" id="submit" type="Submit" value="SUBMIT" readonly style="visibility:hidden;"/></td>
            </tr>
        </table>

    </form>
    <script type="text/javascript" language="javascript">
        document.getElementById("emailAddress").focus();
    </script>
</div>
